import com.fz.Mapper.StudentMapper;
import com.fz.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/6/17.
 */
public class StudentDome {

    protected SqlSessionFactory ss;

    protected SqlSession se;

    @Before
    public void init() {
        String resoures = "mybatis-conf.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resoures);
            this.ss = new SqlSessionFactoryBuilder().build(inputStream);
            this.se = ss.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void colus() {
        se.commit();
        se.close();

    }

    @Test
    public void show() {
        StudentMapper sdao = se.getMapper(StudentMapper.class);

        //调用存储过程删除
//        int f = sdao.del(11);
//        System.out.println(f);


        //调用存储过程插入数据
//        Map<String,Object> ma = new HashMap<String, Object>();
//        ma.put("name","小刘");
//        ma.put("score",22);
//        int ff = sdao.inser(ma);
//        System.out.println(ff);


        //调用有输出参数out的存储过程
//        Map<String, Object> mu = new HashMap<String, Object>();
//        sdao.showstudent(mu);
//        System.out.println(mu.get("r"));
//
//
        //调用无参查询存储过程
//        List<Student> li = sdao.show();
//        for (Student f :li){
//            System.out.println(f.getName());
//        }


//             使用动态sql语句有参和无参方法
//        Map<String,Object> list = new HashMap<String, Object>();
//
//        List<Student> su = sdao.query();
//        for (Student s :su ){
//            System.out.println(s.getName());
//        }
//
//        list.put("name","%白%");
//        List<Student> suu  = sdao.query(list);
//        System.out.println(suu.size());
//
//        list.put("price",23);
//        list.remove("name");
//        List<Student> suuu=sdao.query(list);
//        System.out.println(suuu.size());
//
//

        //使用接口方式
//         List<Student> ff =  sdao.intt(18);
//         System.out.println(ff.size());


        //使用choose 动态语句
//        Map<String,Object> m = new HashMap<String, Object>();
//        List<Student> list = sdao.querys(m);
//        System.out.println(list.size());

        //使用where 动态语句
//        Map<String,Object> m = new HashMap<String, Object>();
//        m.put("name","白小佳");
////        m.put("price",20);
//        List<Student> list = sdao.queryss(m);
//        System.out.println(list.size());


        //使用set动态语句
//        Map<String,Object>  m = new HashMap<String, Object>();
//        m.put("name","bai");
//        m.put("id",16);
//        int list =  sdao.update(m);
//        System.out.println(list);


        //使用foreach动态语句
//        Map<String,Object> m = new HashMap<String, Object>();
//        m.put("ids",new int[]{16,17});
//        System.out.println(sdao.delete(m));


        //使用原生sql语句建库
//        Connection conn = this.se.getConnection();
//        try {
//            PreparedStatement  pre = conn.prepareStatement("create database studnet");
//            pre.execute();
//            pre.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }


    }
}
